package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.sqfw.models.Sqfw_textReplace;
import com.budwk.app.sqfw.services.SqfwTextReplaceService;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/sqfw/textReplace")
@ApiDefinition(tag = "识别文字替换")
@Slf4j
public class SqfwTextReplaceController {
    @Inject
    private SqfwTextReplaceService sqfwTextReplaceService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询识别文字替换")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = Sqfw_textReplace.class
    )
    @ApiResponses(
            implementation = Sqfw_textReplace.class
    )
    @SaCheckPermission("sqfw.text.replace")
    public Result<?> list(@Param("..") Sqfw_textReplace bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getOriginalText() != null && Strings.isNotBlank(bean.getOriginalText().toString())) {
            cnd.and("originalText", "=", bean.getOriginalText());
        }

        if (bean.getReplaceText() != null && Strings.isNotBlank(bean.getReplaceText().toString())) {
            cnd.and("replaceText", "=", bean.getReplaceText());
        }

        if (bean.getReplaceTextLength() != null && Strings.isNotBlank(bean.getReplaceTextLength().toString())) {
            cnd.and("replaceTextLength", "=", bean.getReplaceTextLength());
        }

        if (bean.getType() != null && Strings.isNotBlank(bean.getType().toString())) {
            cnd.and("type", "=", bean.getType());
        }

        if (bean.getOriginalTextSample() != null && Strings.isNotBlank(bean.getOriginalTextSample().toString())) {
            cnd.and("originalTextSample", "=", bean.getOriginalTextSample());
        }

        return Result.data(sqfwTextReplaceService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增识别文字替换")
    @ApiFormParams(
            implementation = Sqfw_textReplace.class
    )
    @ApiResponses
    @SLog(tag = "新增识别文字替换", value = "${args[0]}")
    @SaCheckPermission("sqfw.text.replace.create")
    public Result<?> create(@Param("..") Sqfw_textReplace sqfwTextReplace, HttpServletRequest req) {

        String replaceText = sqfwTextReplace.getReplaceText();
        String originalText = sqfwTextReplace.getOriginalText();
        int count = sqfwTextReplaceService.count(Cnd.NEW().and("original_text", "=", originalText));
        if (count > 0) return Result.error("数据重复");

        String userId = SecurityUtil.getUserId();
        sqfwTextReplace.setCreatedBy(userId);
        sqfwTextReplace.setUpdatedBy(userId);
        int replaceTextLength = originalText.length();
        sqfwTextReplace.setReplaceTextLength(replaceTextLength);

        if (StringUtils.isBlank(replaceText)) sqfwTextReplace.setReplaceText("");
        sqfwTextReplaceService.insert(sqfwTextReplace);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改识别文字替换")
    @ApiFormParams(
            implementation = Sqfw_textReplace.class
    )
    @ApiResponses
    @SLog(tag = "修改识别文字替换", value = "${args[0]}")
    @SaCheckPermission("sqfw.text.replace.update")
    public Result<?> update(@Param("..") Sqfw_textReplace sqfwTextReplace, HttpServletRequest req) {
        sqfwTextReplace.setUpdatedBy(SecurityUtil.getUserId());

        sqfwTextReplace.setReplaceTextLength(sqfwTextReplace.getOriginalText().length());
        sqfwTextReplaceService.updateIgnoreNull(sqfwTextReplace);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取识别文字替换")
    @SLog(tag = "获取识别文字替换", value = "${args[0]}")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckPermission("sqfw.text.replace")
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        Sqfw_textReplace sqfwTextReplace = sqfwTextReplaceService.fetch(id);
        if (sqfwTextReplace == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(sqfwTextReplace);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除识别文字替换")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除识别文字替换", value = "${args[0]}")
    @SaCheckPermission("sqfw.text.replace.delete")
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        Sqfw_textReplace sqfwTextReplace = sqfwTextReplaceService.fetch(id);
        if (sqfwTextReplace == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwTextReplaceService.delete(id);
        req.setAttribute("_slog_msg", sqfwTextReplace.getId());
        return Result.success();
    }

    @At("/replaceText")
    @Ok("json")
    @GET
    @SLog(tag = "文字替换", value = "${args[0]},${args[1]}")
    public Result<?> replaceText(@Param("contentStr") String contentStr, @Param("type") String sourceType, HttpServletRequest req) {
        String newContentStr = sqfwTextReplaceService.replaceText(contentStr, sourceType);
        return Result.success(newContentStr);
    }
}
